<template>
  <div class="login-container pull-height" @keyup.enter.native="handleLogin">
    <div class="login-info text-white animated fadeInLeft">
      <h2 class="login-info-title">电力温度监控系统</h2>
      <ul class="login-info-list">
        <!--<li class="login-info-item">
           <i class="el-icon-check"></i> Avue 是一个基于vue+vuex+vue-router快速后台管理模板，采用token交互验证方式。</li>
        <li class="login-info-item">
          <i class="el-icon-check"></i> 您可以 Avue 为基础，不只限制于vue的页面，你可以嵌入任意第三方网站，基于iframe框架。</li>
        <li class="login-info-item">
          <i class="el-icon-check"></i> Avue 构建简单上手快，最大程度上帮助企业节省时间成本和费用开支。 </li> -->
      </ul>
    </div>
    <div class="login-border pull-height">
      <div class="login-main animated fadeIn">
        <h2>登录 电力温度监控系统</h2>
        <!-- <p>Avue 在线演示系统</p> -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="用户密码" name="user">
              <el-form class="login-form" status-icon :rules="loginRules" ref="loginForm" :model="loginForm" label-width="0">
                <el-form-item prop="username">
                  <el-input @keyup.enter.native="handleLogin" v-model="loginForm.username" auto-complete="off" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input @keyup.enter.native="handleLogin" :type="passwordType" v-model="loginForm.password" auto-complete="off" placeholder="请输入密码">
                    <i class="el-icon-view el-input__icon" slot="suffix" @click="showPassword"></i>
                  </el-input>
                </el-form-item>
                <el-form-item prop="code">
                  <el-row :span="24">
                    <el-col :span="14">
                      <!-- <el-input @keyup.enter.native="handleLogin" :maxlength="code.len" v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码"></el-input> -->
                    </el-col>
                    <el-col :span="10">
                      <div class="login-code">
                        <!-- <span class="login-code-img" @click="refreshCode" v-if="code.type == 'text'">{{code.value}}</span> -->
                        <!-- <img :src="code.src" class="login-code-img" @click="refreshCode" v-else/> -->
                        <!-- <i class="icon-shuaxin login-code-icon" @click="refreshCode"></i> -->
                      </div>
                    </el-col>
                  </el-row>

                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click.native.prevent="handleLogin" class="login-submit">登录</el-button>
                </el-form-item>
              </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'user',
      passwordType: 'password',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false
    }
  },
  methods: {

    showPassword() {
      this.passwordType === ''
        ? (this.passwordType = 'password')
        : (this.passwordType = '')
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/page/Console' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
.hidden{
	display: none;
}
.padding{
	padding: 0 !important;
}

.margin{
	margin: 0 !important;
}

.pull-right {
	float: right !important;;
}

.pull-left {
	float: left !important;
}
.pull-center {
	text-align: center;
}
.pull-flex{
	display: flex;
}

.pull-overflow{
	overflow: hidden;
}
.pull-auto{
	height: auto;
	overflow: hidden;
}
.pull-height{
	height: 100%;
}
.pull-chheight{
	height: calc( 100% - 60px );
}
.pull-fixed{
  position: fixed;
  left: 0;
  top: 0;
}

.text-white{
	color: #fff;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.6);
  position: absolute;
  width: 100%;
}
.login-container::before {
  z-index: -999;
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-image: url("../../assets/img/login.jpg");
  background-size: cover;
}
.login-info {
  padding-left: 60px;
}
.login-info-title {
  line-height: 90px;
}
.login-info-item {
  font-size: 14px;
}
.login-border {
  display: flex;
  align-items: center;
  height: 100%;
  background-color: #fff;
}
.login-main {
  padding: 140px 60px;
  width: 450px;
  border-radius: 3px;
  box-sizing: border-box;
  background-color: #fff;
  & > .el-tabs {
    margin-top: 30px;
  }
}
.login-main > h3 {
  margin-bottom: 20px;
}
.login-main > p {
  color: #76838f;
}
.login-submit {
  width: 100%;
}
.login-form {
  margin: 20px 0;
}
.login-code {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 0 0 10px;
}
.login-code-img {
  width: 100px;
  height: 43px;
  background-color: #eee;
  border: 1px solid #f0f0f0;
  color: #333;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 2px;
  text-indent: 2px;
  text-align: center;
}
</style>
